package com.jilefojing.dao.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import com.jilefojing.beans.enums.forusersession.StatusEnumForUserSession;
import com.jilefojing.beans.pojos.UserSession;
import com.jilefojing.dao.IUserSessionDao;
import com.jilefojing.dao.base.impl.BaseDao;
import com.jilefojing.dao.mapper.IUserSessionMapper;
import com.jilefojing.util.Detect;

/**
 * 
 * @author jgshun
 * 
 */
@Repository
public class UserSessionDao extends BaseDao<UserSession> implements IUserSessionDao {
	@Autowired
	private IUserSessionMapper userSessionMapper;

	@Override
	public UserSession getByUid(String uid) {
		UserSession record = new UserSession();
		record.setUid(uid);
		return super.getOneByRecord(record);
	}

	@Override
	public void deleteByUid(String uid) {
		UserSession record = new UserSession();
		record.setUid(uid);
		super.delete(record);
	}

	@Override
	public List<UserSession> getForUserByStatus(long userId, List<StatusEnumForUserSession> statuses) {
		Example example = new Example(UserSession.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo(UserSession.FIELD_USERID, userId);
		if (Detect.notEmpty(statuses)) {
			Set<Short> statusIds = new HashSet<Short>();
			for (StatusEnumForUserSession _Status : statuses) {
				statusIds.add(_Status.getCode());
			}
			criteria.andIn(UserSession.FIELD_STATUS, new ArrayList<Short>(statusIds));
		}
		return super.getListByExample(example);
	}

	@Override
	public void updateStatusByUids(StatusEnumForUserSession status, List<String> uids) {
		userSessionMapper.updateStatusByUids(status, uids);
	}
}
